package cn.kgc.javaweb.dao;

import cn.kgc.javaweb.entity.User;
import cn.kgc.javaweb.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author: mosin
 * @version: v1.0  2022/3/24
 */
public class UserDaoImpl implements UserDao {

    //添加用户
    @Override
    public int insert(User user) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "insert into user (username,password)values(?,?)";
        try {
            int line = queryRunner.execute(sql, user.getUsername(), user.getPassword());
            return line;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
    //根据用户名和密码查询用户 登录判定
    @Override
    public User selectByUser(User user) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select * from user where username = ? and password = ?";
        try {
            User usr = queryRunner.query(sql, new BeanHandler<>(User.class), user.getUsername(), user.getPassword());
            return usr;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
